package com.tc.shiro.service.impl;

import com.tc.shiro.entity.PortalOperator;
import com.tc.shiro.model.LoginReqParam;
import com.tc.shiro.model.LoginResParam;
import com.tc.shiro.repository.PortalOperatorRepository;
import com.tc.shiro.service.LoginService;
import com.tc.shiro.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private PortalOperatorRepository portalOperatorRepo;

    @Override
    public LoginResParam login(LoginReqParam loginReqParam) { //验证密码 是否成功  摘要(密码+盐值)
        PortalOperator operatorSelect = new PortalOperator();
        operatorSelect.setOperator(loginReqParam.getLoginName());
        PortalOperator portalOperator = portalOperatorRepo.selectByOperator(operatorSelect);
        if(portalOperator==null){
            createResult("9999","用户不存在");
        }
        try {
            String digest = SecurityUtils.decryptAndDigest(loginReqParam.getLoginPwd(), portalOperator.getSalt());
            if(portalOperator.getLoginPassword().equals(digest)){
               return createResult("0000","登录成功");
            }
            return createResult("9999","用户名或密码错误");
        }catch (Exception e){
            return createResult("9999","登录异常");
        }
    }
    private LoginResParam createResult(String code,String mesage){
        LoginResParam lp=new LoginResParam();
        lp.setRespCode(code);
        lp.setRespMsg(mesage);
        return lp;
    }
}
